package com.lagou.edu.ad.remote;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lagou.edu.ad.entity.PromotionAd;
import com.lagou.edu.ad.entity.PromotionSpace;
import com.lagou.edu.ad.service.IPromotionAdService;
import com.lagou.edu.ad.service.IPromotionSpaceService;
import com.lagou.edu.dto.PromotionAdDTO;
import com.lagou.edu.dto.PromotionSpaceDTO;
import com.lagou.edu.remote.AdRemoteService;
import com.lagou.edu.reponse.ResponseDTO;
import com.lagou.edu.util.ConverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/ad")
public class AdService implements AdRemoteService {

    @Autowired
    private IPromotionSpaceService promotionSpaceService;

    @Autowired
    private IPromotionAdService promotionAdService;

    @GetMapping("/space/getAllSpaces")
    public List<PromotionSpaceDTO> getAllSpaces() {
        List<PromotionSpace> promotionSpaceList = promotionSpaceService.list();
        return ConverUtil.convertList(promotionSpaceList, PromotionSpaceDTO.class);
    }

    @GetMapping("/space/getAdSpaceById")
    public PromotionSpaceDTO getAdSpaceById(Integer id) {
        PromotionSpace promotionSpace = promotionSpaceService.getById(id);
        return ConverUtil.convert(promotionSpace, PromotionSpaceDTO.class);
    }

    @PostMapping("/space/saveOrUpdateSpace")
    public ResponseDTO saveOrUpdateSpace(PromotionSpaceDTO promotionSpaceDTO) {

        PromotionSpace promotionSpace = ConverUtil.convert(promotionSpaceDTO, PromotionSpace.class);
        if (promotionSpace == null) {
            return ResponseDTO.ofError("参数为空");
        }

        Date now = new Date();
        if (promotionSpace.getId() == null) {
            promotionSpace.setCreateTime(now);
            promotionSpace.setUpdateTime(now);
            promotionSpace.setIsDel(0);
        } else {
            promotionSpace.setUpdateTime(now);
        }

        try {
            promotionSpaceService.saveOrUpdate(promotionSpace);
            return ResponseDTO.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseDTO.ofError(e.getMessage());
        }

    }

    @GetMapping("/getAdBySpaceKey")
    public List<PromotionSpaceDTO> getAdBySpaceKey(String[] keys) {

        List<PromotionSpaceDTO> promotionSpaceDTOS = new ArrayList<>();

        for (String key : keys) {

            QueryWrapper<PromotionSpace> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("spaceKey", key);
            // 查询广告位
            PromotionSpace promotionSpace = promotionSpaceService.getOne(queryWrapper);
            PromotionSpaceDTO promotionSpaceDTO = ConverUtil.convert(promotionSpace, PromotionSpaceDTO.class);
            if (promotionSpaceDTO != null) {

                // 获取广告
                QueryWrapper<PromotionAd> promotionAdQueryWrapper = new QueryWrapper<>();
                promotionAdQueryWrapper.eq("spaceId", promotionSpace.getId());
                // 前台广告分为顶部广告和侧边栏广告.前台根据广告位的编号,获取对应广告位的在有效期内的广告并且状态为上架状态的广告.顶部广告可以展示多个,以轮播图形式展示
                promotionAdQueryWrapper.eq("status", 1);
                Date now = new Date();
                // startTime < ? AND endTime > ?
                promotionAdQueryWrapper.lt("startTime", now);
                promotionAdQueryWrapper.gt("endTime", now);
                List<PromotionAd> promotionAdList = promotionAdService.list(promotionAdQueryWrapper);

                List<PromotionAdDTO> promotionAdDTOList = ConverUtil.convertList(promotionAdList, PromotionAdDTO.class);
                promotionSpaceDTO.setPromotionDTOS(promotionAdDTOList);
            }
            promotionSpaceDTOS.add(promotionSpaceDTO);
        }

        return promotionSpaceDTOS;
    }

    @GetMapping("/getAdList")
    public List<PromotionAdDTO> getAdList() {
        List<PromotionAd> promotionAdList = promotionAdService.list();
        return ConverUtil.convertList(promotionAdList, PromotionAdDTO.class);
    }

    @PostMapping("/saveOrUpdateAd")
    public ResponseDTO saveOrUpdateAd(PromotionAdDTO promotionAdDTO) {

        PromotionAd promotionSpace = ConverUtil.convert(promotionAdDTO, PromotionAd.class);
        if (promotionSpace == null) {
            return ResponseDTO.ofError("参数为空");
        }

        Date now = new Date();
        if (promotionSpace.getId() == null) {
            promotionSpace.setCreateTime(now);
            promotionSpace.setUpdateTime(now);
            // 1：上架，0下架
            promotionSpace.setStatus(1);
        } else {
            promotionSpace.setUpdateTime(now);
        }

        try {
            promotionAdService.saveOrUpdate(promotionSpace);
            return ResponseDTO.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseDTO.ofError(e.getMessage());
        }

    }

    @GetMapping("/getAdById")
    public PromotionAdDTO getAdById(Integer id) {
        PromotionAd promotionAd = promotionAdService.getById(id);
        return ConverUtil.convert(promotionAd, PromotionAdDTO.class);
    }
}